package org.blackcat.web;

import org.blackcat.common.DecimalUtils;
import org.blackcat.domain.BloodPressure;
import org.blackcat.domain.ECG;
import org.blackcat.domain.Temperature;
import org.blackcat.domain.User;
import org.blackcat.domain.common.MsgResponse;
import org.blackcat.service.BloodPressureService;
import org.blackcat.service.ECGService;
import org.blackcat.service.TemperatureService;
import org.blackcat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author 詹泽
 * @date 2017/2/22
 */
@Component
public class AutomationController {

    @Autowired
    private TemperatureService temperatureService;

    @Autowired
    private BloodPressureService bloodPressureService;

    @Autowired
    private ECGService ecgService;

    @Autowired
    private UserService userService;

    //温度
    @Scheduled(fixedRate = 5*60*60*1000,initialDelay = 10*1000)
    public void addTemperature() throws Exception {
        Temperature temperature = new Temperature();
        double min = 35D;
        double max = 42D;
        temperature.setSize(DecimalUtils.add(DecimalUtils.mul(new Random().nextDouble(),(max - min),1),min));
        System.out.println(temperature.getSize());
        MsgResponse<User> m = userService.findByAccount("admin","admin");
        String token = m.getData().getToken();
        try {
            temperatureService.addTemerature(temperature,token);
        } catch (Exception e) {
            e.printStackTrace();
        }
    };

    //血压
    @Scheduled(fixedRate = 5*60*60*1000,initialDelay = 2*1000)
    public void addBloodPressure() throws Exception {
        Random random = new Random();
        BloodPressure bloodPressure = new BloodPressure();
        bloodPressure.setHighPressure(random.nextInt(150)%(160-80+1) + 80);
        bloodPressure.setLowPressure(random.nextInt(100)%(100-60+1) + 60);
        bloodPressure.setPulse(random.nextInt(90)%(90-50+1) + 50);
        MsgResponse<User> m = userService.findByAccount("admin","admin");
        String token = m.getData().getToken();
        try {
            bloodPressureService.addBloodPressure(bloodPressure,token);
        } catch (Exception e) {
            e.printStackTrace();
        }
    };
//    //心电
//    @Scheduled(fixedRate = 5*60*60*1000,initialDelay = 2*1000)
//    public void addECG() throws Exception {
//        StringBuilder sb = new StringBuilder();
//        Random random = new Random();
//        for(int i = 0; i < 100; i++){
//            sb.append(random.nextInt(10)%(10-6+1) + 6);
//        }
//        ECG ecg = new ECG();
//        ecg.setContent(sb.toString());
//        MsgResponse<User> m = userService.findByAccount("admin","admin");
//        String token = m.getData().getToken();
//        try {
//            ecgService.addECG(ecg,token);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    };

}
